home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-25 | 18.4 KB | 674 lines | [TEXT/CWIE] |
- // ==================================================
- // CTouchMeMainWindow.cp
- // Copyright (C) 1996-1997 Mizutori Tetsuya
- // July 4, 1996; February 3, 1997; April 23, 1997.
- // ==================================================
- // All documents are pretty-printed in 10-point Geneva font.
-
- #include <LDialogBox.h>
- #include <LDragTask.h>
- #include <LStdControl.h>
- #include <LEditField.h>
- #include <LCaption.h>
- #include <LBroadcaster.h>
- #include <UDrawingState.h>
- #include <UDrawingUtils.h>
- #include <UMemoryMgr.h>
- #include <UDebugging.h>
- #include <LString.h>
-
- #include <PP_Messages.h>
- #include <UReanimator.h>
-
- #include "touchMeConstants.h"
- #include "CTouchMeMainWindow.h"
- #include "CRadioButton.h"
- #include "CDateEditField.h"
- #include "CTouchMePref.h"
- #include "UFileTools.h"
- #include "UDateTime.h"
- #include "UMacOSTools.h"
-
-
- #define isButtonOn( xState ) ( (xState) == Button_On )
- #define nButtonValue( xBoolean ) ( (xBoolean) ? Button_On : Button_Off )
-
-
- // === Static Class Variables ===
- //Boolean CTouchMeMainWindow::sGlobalModifier = false;
-
-
- // --------------------------------------------------
- // • CreateTouchMeMainWindowStream [static]
- // --------------------------------------------------
-
- CTouchMeMainWindow *
- CTouchMeMainWindow::CreateTouchMeMainWindowStream(
- LStream * inStream )
- {
- return new CTouchMeMainWindow( inStream );
- }
-
-
- // --------------------------------------------------
- // • CTouchMeMainWindow(LStream *)
- // --------------------------------------------------
-
- CTouchMeMainWindow::CTouchMeMainWindow(
- LStream * inStream )
- : LDialogBox( inStream ),
- LDragAndDrop( UQDGlobals::GetCurrentPort(), this )
- {
- mGlobalModifier = false;
-
- // For the drag & drop operations.
- mModifier = false;
- mTextDrag = false;
- mHilitePaneID = kNoPaneID;
- }
-
-
- // --------------------------------------------------
- // • ~CTouchMeMainWindow
- // --------------------------------------------------
-
- CTouchMeMainWindow::~CTouchMeMainWindow()
- {
- }
-
-
- // ==================================================
- // Member functions
- // ==================================================
-
- // --------------------------------------------------
- // • FinishCreateSelf
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::FinishCreateSelf()
- {
- LDialogBox::FinishCreateSelf();
-
- UReanimator::LinkListenerToControls( this, this, rRidL_TouchMeMainWindow );
-
- // This DialogBox listens to myself for the messages by a drag & drop operation.
- this->AddListener( this );
-
- // Add listeners of CRadioButtons to broadcaster of LStdCheckBox.
- // When the LStdCheckBox has changed, then change message will be sent to CRadioButtons.
- LStdCheckBox * theCheckBox;
- CRadioButton * theRadio;
- PaneIDT thePane;
-
- theCheckBox = (LStdCheckBox *) FindPaneByID( kMain_CrCheckbox );
- for ( long i = 0; (thePane = kRadioGroupCreation[i]) != kNoPaneID; i++ ) {
- theRadio = (CRadioButton *) FindPaneByID( thePane );
- theCheckBox->AddListener( theRadio );
- }
-
- theCheckBox = (LStdCheckBox *) FindPaneByID( kMain_MdCheckbox );
- for ( long i = 0; (thePane = kRadioGroupModification[i]) != kNoPaneID; i++ ) {
- theRadio = (CRadioButton *) FindPaneByID( thePane );
- theCheckBox->AddListener( theRadio );
- }
-
- }
-
-
- // --------------------------------------------------
- // • DrawSelf
- // --------------------------------------------------
- // Draw my additional custom figures in the dialog window.
- // This procedure erases the 2-pixel width region near the bound.
- // The reason is that the 2-pixel region may not be hilited properly
- // during a drag & drop because my window is colored at background.
- // ->See HiliteDropArea(). We call ApplyForeAndBackColors() in it, instead.
-
- void
- CTouchMeMainWindow::DrawSelf( void )
- {
- // StColorPenState theCurrentState;
- // LDialogBox::DrawSelf();
- // StColorPenState::Normalize();
-
- // Set the foreground and background colors of the current port.
- ApplyForeAndBackColors();
-
- // Draw a very tiny spot at the top-left corner of the window.
- Rect theRect;
- ::SetRect( &theRect, 3, 3, 8, 8 ); // (left,top,right,bottom)
-
- if ( mGlobalModifier ) ::FillRect( &theRect, &(qd.gray) );
- else ::EraseRect( &theRect );
-
- #ifdef COMMENT
- RgnHandle theRgnH = ::NewRgn();
- RgnHandle theInRgnH = ::NewRgn();
-
- Rect theRect;
- CalcLocalFrameRect( theRect );
- ::RectRgn( theRgnH, &theRect );
- ::InsetRect( &theRect, 2, 2);
- ::RectRgn( theInRgnH, &theRect );
- ::DiffRgn( theRgnH, theInRgnH, theRgnH );
-
- ::EraseRgn( theRgnH );
-
- ::DisposeRgn( theRgnH );
- ::DisposeRgn( theInRgnH );
- #endif // COMMENT
- }
-
-
- // --------------------------------------------------
- // • FindCommandStatus
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::FindCommandStatus(
- CommandT inCommand,
- Boolean & outEnabled,
- Boolean & outUsesMark,
- Char16 & outMark,
- Str255 outName )
- {
- #pragma unused ( outUsesMark, outMark, outName )
-
- switch (inCommand) {
- case cmd_About:
- case cmd_Open:
- case cmd_Close:
- case cmd_Save:
- case cmd_Quit:
- outEnabled = true;
- break;
- case cmd_New:
- default:
- outEnabled = false;
- break;
- }
- }
-
-
-
- // --------------------------------------------------
- // • ListenToMessage
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::ListenToMessage(
- MessageT inMessage,
- void * ioParam )
- {
- switch ( inMessage ) {
-
- case msg_Main_ButtonOK:
- {
- // Store the preference data to Prefs file.
- ProcessCommand( msg_SavePrefs, ioParam );
- ProcessCommand( msg_Main_ButtonOK, ioParam );
- // LDialogBox::ListenToMessage( inMessage, ioParam );
- }
- break;
-
- case msg_Main_ButtonCencel:
- {
- // Discard the preference data.
- ProcessCommand( msg_SavePrefsWFrame, ioParam );
- ProcessCommand( msg_Main_ButtonCencel, ioParam );
- // LDialogBox::ListenToMessage( inMessage, ioParam );
- }
- break;
-
- case cmd_Save:
- {
- // Save the preferences data.
- ProcessCommand( msg_SavePrefs, ioParam );
- }
- break;
-
- case msg_Main_ButtonHelp:
- {
- // Show the help window.
- ProcessCommand( msg_Help, ioParam );
- }
- break;
-
- case msg_Main_Title:
- {
- // Show the About window.
- // But this does not work because 'Title' pane is not enabled now.
- ProcessCommand( cmd_About, ioParam );
- }
- break;
-
- case msg_Main_CrButtonNow:
- {
- // Set up creation date time EditFields to the current date time.
- unsigned long theDateTimeSecs;
- ::GetDateTime( &theDateTimeSecs );
-
- if ( mGlobalModifier ) {
- Str63 dateStr, timeStr;
- UDateTime::SecondsToDateTimeString(theDateTimeSecs,dateStr,timeStr);
- LString::CopyPStr( "\p0:0:0", timeStr );
- UDateTime::DateTimeStringToSeconds(dateStr,timeStr,theDateTimeSecs);
- }
-
- SetEditFieldDateTime( kMain_CrTextEditDate, theDateTimeSecs );
- SetEditFieldDateTime( kMain_CrTextEditTime, theDateTimeSecs );
-
- if ( GetValueForPaneID( kMain_ButtonSync ) == Button_On ) {
- SetEditFieldDateTime( kMain_MdTextEditDate, theDateTimeSecs );
- SetEditFieldDateTime( kMain_MdTextEditTime, theDateTimeSecs );
- }
- }
- break;
-
- case msg_Main_MdButtonNow:
- {
- // Set up modification date time EditFields to the current date time.
- unsigned long theDateTimeSecs;
- ::GetDateTime( &theDateTimeSecs );
-
- if ( mGlobalModifier ) {
- Str63 dateStr, timeStr;
- UDateTime::SecondsToDateTimeString(theDateTimeSecs,dateStr,timeStr);
- LString::CopyPStr( "\p0:0:0", timeStr );
- UDateTime::DateTimeStringToSeconds(dateStr,timeStr,theDateTimeSecs);
- }
-
- SetEditFieldDateTime( kMain_MdTextEditDate, theDateTimeSecs );
- SetEditFieldDateTime( kMain_MdTextEditTime, theDateTimeSecs );
-
- if ( GetValueForPaneID( kMain_ButtonSync ) == Button_On ) {
- SetEditFieldDateTime( kMain_CrTextEditDate, theDateTimeSecs );
- SetEditFieldDateTime( kMain_CrTextEditTime, theDateTimeSecs );
- }
- }
- break;
-
- case msg_Main_DroppedFile:
- {
- // Setup date time EditFields according to the date time stamp of the dropped file.
- // Parameter: FSSpec *ioParam
- FSSpec * theFSSpec = (FSSpec *) ioParam;
- unsigned long theCrDateTime = 0, theMdDateTime = 0;
- UFileTools::GetFSSpecDateTime( *theFSSpec, theCrDateTime, theMdDateTime );
-
- SetEditFieldDateTime( kMain_CrTextEditDate, theCrDateTime );
- SetEditFieldDateTime( kMain_CrTextEditTime, theCrDateTime );
-
- SetEditFieldDateTime( kMain_MdTextEditDate, theMdDateTime );
- SetEditFieldDateTime( kMain_MdTextEditTime, theMdDateTime );
- }
- break;
-
- case msg_Main_DroppedFileCrDate:
- case msg_Main_DroppedFileCrTime:
- case msg_Main_DroppedFileMdDate:
- case msg_Main_DroppedFileMdTime:
- {
- // Setup date time EditFields separately according to the dropped file.
- FSSpec * theFSSpec = (FSSpec *) ioParam;
- unsigned long theCrDateTime = 0, theMdDateTime = 0;
- UFileTools::GetFSSpecDateTime( *theFSSpec, theCrDateTime, theMdDateTime );
-
- switch ( inMessage ) {
- case msg_Main_DroppedFileCrDate:
- SetEditFieldDateTime( kMain_CrTextEditDate, theCrDateTime ); break;
- case msg_Main_DroppedFileCrTime:
- SetEditFieldDateTime( kMain_CrTextEditTime, theCrDateTime ); break;
- case msg_Main_DroppedFileMdDate:
- SetEditFieldDateTime( kMain_MdTextEditDate, theMdDateTime ); break;
- case msg_Main_DroppedFileMdTime:
- SetEditFieldDateTime( kMain_MdTextEditTime, theMdDateTime ); break;
- }
- }
- break;
-
- case msg_Main_DroppedTextCrDate:
- case msg_Main_DroppedTextCrTime:
- case msg_Main_DroppedTextMdDate:
- case msg_Main_DroppedTextMdTime:
- {
- // Setup date time EditFields separately according to the dropped 'TEXT'.
- StringPtr str = (StringPtr) ioParam;
-
- switch ( inMessage ) {
- case msg_Main_DroppedTextCrDate:
- SetEditFieldText( kMain_CrTextEditDate, str); break;
- case msg_Main_DroppedTextCrTime:
- SetEditFieldText( kMain_CrTextEditTime, str); break;
- case msg_Main_DroppedTextMdDate:
- SetEditFieldText( kMain_MdTextEditDate, str); break;
- case msg_Main_DroppedTextMdTime:
- SetEditFieldText( kMain_MdTextEditTime, str); break;
- }
- }
- break;
-
- case msg_Main_CrCheckbox:
- case msg_Main_MdCheckbox:
- {
- // Do nothing here.
- }
- break;
-
- case msg_Main_CrButtonCurrent:
- case msg_Main_CrButtonDirect:
- case msg_Main_CrButtonFirst:
- case msg_Main_CrButtonSecond:
- case msg_Main_MdButtonCurrent:
- case msg_Main_MdButtonDirect:
- case msg_Main_MdButtonFirst:
- case msg_Main_MdButtonSecond:
- {
- // Notice that the value of the message is same to the pane ID of the radio button.
- PaneIDT thePane = (PaneIDT) inMessage;
- PaneIDT theOtherPane = GetPairedPaneID( thePane );
- if ( GetValueForPaneID( kMain_ButtonSync ) == Button_On )
- if ( GetValueForPaneID( thePane ) == Button_On )
- SetValueForPaneID( theOtherPane, Button_On );
- }
- break;
-
- default:
- {
- LDialogBox::ListenToMessage( inMessage, ioParam );
- break;
- }
-
- }
- }
-
-
- // --------------------------------------------------
- // • AttemptClose
- // --------------------------------------------------
- // Quit program like as "Cancel" button if "Close Box" was selected.
-
- void
- CTouchMeMainWindow::AttemptClose( void )
- {
- //LDialogBox::AttemptClose();
-
- BroadcastMessage( msg_Main_ButtonCencel, (void *) nil );
- }
-
-
- // ==================================================
- // Member functions (read or write dialog settings)
- // ==================================================
-
- // --------------------------------------------------
- // • SetupDialog
- // --------------------------------------------------
- // Setup dialog status according to the member variables mPrefs.
-
- void
- CTouchMeMainWindow::SetupDialog(
- const SPrefsRec & inPrefs )
- {
- // Set the window position.
- Rect theWindowRect = inPrefs.wframe;
- if ( ! ::EmptyRect( &theWindowRect ) ) {
- DoSetPosition( topLeft( theWindowRect ));
- }
-
- SetValueForPaneID( kMain_CrCheckbox,
- nButtonValue( inPrefs.enabled[ touchType_CreationDate - touchType_CreationDate ] ) );
-
- SetValueForPaneID( kMain_MdCheckbox,
- nButtonValue( inPrefs.enabled[ touchType_ModificationDate - touchType_CreationDate ] ) );
-
- SetValueForPaneID( kMain_ButtonSync, nButtonValue( inPrefs.sync ) );
-
-
- // Because we're about to set the value for controls
- // we're listening to we'll turn off listening temporarily.
- // Otherwise we'll get in a tug-of-war
- // as the controls broadcast their changing values.
- StopListening();
-
- PaneIDT thePane;
- for ( long i = touchFlag_Current; (thePane = kRadioGroupCreation[i-touchFlag_Current]) != kNoPaneID; i++ ) {
- SetValueForPaneID( thePane, nButtonValue(false) );
- if ( i == inPrefs.flag[touchType_CreationDate - touchType_CreationDate] )
- SetValueForPaneID( thePane, nButtonValue(true) );
- }
-
- for ( long i = touchFlag_Current; (thePane = kRadioGroupModification[i-touchFlag_Current]) != kNoPaneID; i++ ) {
- SetValueForPaneID( thePane, nButtonValue(false) );
- if ( i == inPrefs.flag[touchType_ModificationDate - touchType_CreationDate] )
- SetValueForPaneID( thePane, nButtonValue(true) );
- }
-
- // Start listening again.
- StartListening();
-
-
- Str255 strDate, strTime;
-
- UDateTime::SecondsToDateTimeString(
- inPrefs.datetime[ touchType_CreationDate - touchType_CreationDate ], strDate, strTime );
- SetEditFieldText( kMain_CrTextEditDate, strDate);
- SetEditFieldText( kMain_CrTextEditTime, strTime);
-
- UDateTime::SecondsToDateTimeString(
- inPrefs.datetime[ touchType_ModificationDate - touchType_CreationDate ], strDate, strTime );
- SetEditFieldText( kMain_MdTextEditDate, strDate);
- SetEditFieldText( kMain_MdTextEditTime, strTime);
- }
-
-
- // --------------------------------------------------
- // • InspectDialog
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::InspectDialog(
- SPrefsRec & outPrefs )
- {
- // Get the window position.
- Rect theWindowRect;
- GetGlobalFrameRect( theWindowRect );
- outPrefs.wframe = theWindowRect;
-
- outPrefs.enabled[ touchType_CreationDate - touchType_CreationDate] =
- isButtonOn( GetValueForPaneID( kMain_CrCheckbox ) );
-
- outPrefs.enabled[ touchType_ModificationDate - touchType_CreationDate] =
- isButtonOn( GetValueForPaneID( kMain_MdCheckbox ) );
-
- outPrefs.sync = isButtonOn( GetValueForPaneID( kMain_ButtonSync ) );
-
-
- PaneIDT thePane;
- outPrefs.flag[touchType_CreationDate - touchType_CreationDate] = touchFlag_Current;
- for ( long i = touchFlag_Current; (thePane = kRadioGroupCreation[i-touchFlag_Current]) != kNoPaneID; i++ ) {
- if ( isButtonOn( GetValueForPaneID(thePane) ) ) {
- outPrefs.flag[touchType_CreationDate - touchType_CreationDate] = i;
- break;
- }
- }
-
- outPrefs.flag[touchType_ModificationDate - touchType_CreationDate] = touchFlag_Current;
- for ( long i = touchFlag_Current; (thePane = kRadioGroupModification[i-touchFlag_Current]) != kNoPaneID; i++ ) {
- if ( isButtonOn( GetValueForPaneID(thePane) ) ) {
- outPrefs.flag[touchType_ModificationDate - touchType_CreationDate] = i;
- break;
- }
- }
-
- Str255 strDate, strTime;
-
- GetEditFieldText( kMain_CrTextEditDate, strDate );
- GetEditFieldText( kMain_CrTextEditTime, strTime);
- UDateTime::DateTimeStringToSeconds( strDate, strTime,
- outPrefs.datetime[ touchType_CreationDate - touchType_CreationDate ] );
-
- GetEditFieldText( kMain_MdTextEditDate, strDate);
- GetEditFieldText( kMain_MdTextEditTime, strTime);
- UDateTime::DateTimeStringToSeconds( strDate, strTime,
- outPrefs.datetime[ touchType_ModificationDate - touchType_CreationDate ] );
- }
-
-
-
- // ==================================================
- // Member functions
- // ==================================================
-
- // --------------------------------------------------
- // • GetGlobalFrameRect
- // --------------------------------------------------
- // Get the global frame rect, a bound of dialog
-
- void
- CTouchMeMainWindow::GetGlobalFrameRect(
- Rect & outRect )
- {
- CalcPortFrameRect( outRect );
- PortToGlobalPoint( topLeft( outRect ) );
- PortToGlobalPoint( botRight( outRect ) );
- }
-
-
- // --------------------------------------------------
- // • MoveWindow
- // --------------------------------------------------
- // Move the dialog window
-
- void
- CTouchMeMainWindow::MoveWindow(
- Rect & inRect )
- {
- if ( ! ::EmptyRect( &inRect ) ) {
- DoSetPosition( topLeft( inRect ));
- }
- }
-
-
-
- // ==================================================
- // Common functions
- // ==================================================
-
- // --------------------------------------------------
- // • SetEditFieldText
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::SetEditFieldText(
- PaneIDT inPane,
- const Str255 inString,
- Boolean toSelectAll ) //= false
- {
- CDateEditField * theEditField;
-
- theEditField = (CDateEditField *) FindPaneByID( inPane );
- theEditField->SetText( inString );
-
- if ( toSelectAll ) theEditField->SelectAll();
- }
-
-
- // --------------------------------------------------
- // • SetEditFieldTextRefresh
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::SetEditFieldTextRefresh(
- PaneIDT inPane,
- const Str255 inString,
- Boolean toSelectAll ) //= false
- {
- CDateEditField * theEditField;
-
- theEditField = (CDateEditField *) FindPaneByID( inPane );
- theEditField->SetTextAndRefresh( inString );
-
- if ( toSelectAll ) theEditField->SelectAll();
- }
-
-
- // --------------------------------------------------
- // • GetEditFieldText
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::GetEditFieldText(
- PaneIDT inPane,
- Str255 outString,
- Boolean toSelectAll ) //= false
- {
- CDateEditField * theEditField;
-
- theEditField = (CDateEditField *) FindPaneByID( inPane );
- theEditField->GetText( outString );
-
- if ( toSelectAll ) theEditField->SelectAll();
- }
-
-
- // --------------------------------------------------
- // • SetEditFieldDateTime
- // --------------------------------------------------
-
- void
- CTouchMeMainWindow::SetEditFieldDateTime(
- PaneIDT inPane,
- const unsigned long inDateTimeSeconds,
- Boolean toSelectAll ) //= false
- {
- CDateEditField * theEditField;
-
- theEditField = (CDateEditField *) FindPaneByID( inPane );
- theEditField->SetDateTime( inDateTimeSeconds );
-
- if ( toSelectAll ) theEditField->SelectAll();
- }
-
-
- // --------------------------------------------------
- // • GetPairedPaneID
- // --------------------------------------------------
-
- PaneIDT
- CTouchMeMainWindow::GetPairedPaneID(
- const PaneIDT inPaneID )
- {
- PaneIDT thePane;
-
- for ( long i=0; (thePane = kRadioGroupCreation[i]) != kNoPaneID; i++ ) {
- if ( thePane == inPaneID ) return kRadioGroupModification[i];
- }
-
- for ( long i=0; (thePane = kRadioGroupModification[i]) != kNoPaneID; i++ ) {
- if ( thePane == inPaneID ) return kRadioGroupCreation[i];
- }
-
- return kNoPaneID;
- }
-
-
- // --------------------------------------------------
- // • Indicator
- // --------------------------------------------------
- // I prefer to show a very tiny spot at the corner to indicate the status.
-
- void
- CTouchMeMainWindow::Indicator(
- const Boolean inStatus )
- {
- if ( inStatus != mGlobalModifier ) {
- mGlobalModifier = inStatus;
- if ( FocusDraw() ) DrawSelf();
- }
- }
-
-
- // end of program
-